import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    keywords:["粉色","粉红色","粉嘟嘟","绿色","绿头发","紫色","蓝色"],
    recommand:[],
    history:[]
  },
  mutations: {
    CHANGE_HISTORY(state,key){
      //首先 去重 
      var index = state.history.findIndex((element)=>{
        return element === key
      });

      if(index !== -1){
        state.history.splice(index,1);
      }
      state.history.unshift(key);
      
      if(state.history.length > 4){
        state.history.pop();
      }
    },
    FILTER_KEYWORDS(state,key){
      state.recommand = [];
      state.keywords.forEach((element)=>{
        if(element.includes(key)){
          state.recommand.push(element)
          console.log(state.recommand)
        }
      })
    }
  },
  actions: {
    FILTER_KEYWORDS(context,key){
      context.commit("FILTER_KEYWORDS",key)
    },
    CHANGE_HISTORY(context,key){
      context.commit("CHANGE_HISTORY",key)
    }
  },
  modules: {
  }
})


